package com.platform.module.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.core.text.Convert;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.module.domain.LinkConcentratorMeter;
import com.platform.module.mapper.LinkConcentratorMeterMapper;
import com.platform.module.service.ILinkConcentratorMeterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

/**
 * 集中器和热关联Service业务层处理
 * 
 * @author wangqi
 * @date 2020-08-01
 */
@Service
public class LinkConcentratorMeterServiceImpl extends ServiceImpl<LinkConcentratorMeterMapper,LinkConcentratorMeter> implements ILinkConcentratorMeterService
{
    @Autowired
    private LinkConcentratorMeterMapper linkConcentratorMeterMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 将关联信息保存redis
     */
    @PostConstruct
    public void init() {
        List<LinkConcentratorMeter> list = linkConcentratorMeterMapper.selectList(new QueryWrapper<>());
        for(LinkConcentratorMeter concentratorMeter : list){
            redisUtil.hset("concentratorMeter", concentratorMeter.getCalorimeterId(), concentratorMeter.getConcentratorId());
        }
    }

    /**
     * 新增集中器和热关联
     *
     * @param linkConcentratorMeter 集中器和热关联
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLinkConcentratorMeter(LinkConcentratorMeter linkConcentratorMeter)
    {
        redisUtil.hset("concentratorMeter", linkConcentratorMeter.getCalorimeterId(), linkConcentratorMeter.getConcentratorId());
        return linkConcentratorMeterMapper.insert(linkConcentratorMeter);
    }

    /**
     * 修改集中器和热关联
     *
     * @param linkConcentratorMeter 集中器和热关联
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLinkConcentratorMeter(LinkConcentratorMeter linkConcentratorMeter)
    {
        redisUtil.hset("concentratorMeter", linkConcentratorMeter.getCalorimeterId(), linkConcentratorMeter.getConcentratorId());
        return linkConcentratorMeterMapper.updateConcentratorMeter(linkConcentratorMeter);
    }
}
